using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace WindowsGame1
{

    public class Game1 : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;

        Tiles[,] tiles = new Tiles[19, 15]; //Rows and Columns
                                            //The Array to store each tiles of image and position.
        int offsetX =0;
        int offsetY =0;

        Texture2D red;
        Texture2D blue;
        Texture2D green;

        int Is_selected = 0;
        int previous_x;
        int previous_y;

        public Game1()
        {
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";
        }


        protected override void Initialize()
        {
            this.IsMouseVisible = true;


            base.Initialize();
        }


        protected override void LoadContent()
        {
            
            spriteBatch = new SpriteBatch(GraphicsDevice);

             blue = Content.Load<Texture2D>("blue");
             green = Content.Load<Texture2D>("green");
             red = Content.Load<Texture2D>("red");

            Random random = new Random();
            ///
            ///Assign the image to each of tiles 
            ///
            for (int x = 0; x < this.tiles.GetLength(0); x++)
            {
                for (int y = 0; y < this.tiles.GetLength(1); y++)
                {
                    switch (random.Next(0,9)) //green a lot or not (0,adjust)
                    {
                        case 0: this.tiles[x, y] = new Tiles(green, new Vector2(x, y)); break;
                        default:
                            this.tiles[x, y] = new Tiles(blue, new Vector2(x, y));
                           // this.tiles[x, y]._LandPossible = true; // the unit can land or not for future implenment
                            break;
                    }
                }
            }

            
        }

        protected override void UnloadContent()
        {
            
        }


        protected override void Update(GameTime gameTime)
        {

            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
                this.Exit();

            base.Update(gameTime);
        }

        /// <summary>
        /// InMouseclick, we need to consider that only one tile that can be selected. Therefore, there is one "if" to check that.
        /// </summary>
        /// <param name="gameTime"></param>
        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.Black);
            this.spriteBatch.Begin();

            for (int y = 0; y < this.tiles.GetLength(1); y++)
            {
                for (int x = 0; x < this.tiles.GetLength(0); x++)
                {
                    this.spriteBatch.Draw(this.tiles[x, y]._Texture, new Vector2(x * 40 + y % 2 * 20 +this.offsetX, y * 30 + this.offsetY), Color.White);
                     if(IsMousePass(x, y))
                     {
                         
                         spriteBatch.Draw(red, new Vector2(x * 40 + y % 2 * 20 + this.offsetX, y * 30 + this.offsetY), Color.White);
                     }
                     if(IsMouseClick(x,y))
                     {
                         if (Is_selected == 0)
                         {
                             Is_selected = 1;
                             previous_x = x;
                             previous_y = y;
                             this.tiles[x, y]._Texture = red;
                             spriteBatch.Draw(this.tiles[x, y]._Texture, new Vector2(x * 40 + y % 2 * 20 + this.offsetX, y * 30 + this.offsetY), Color.White);
                         }
                         else if (Is_selected == 1)
                         {
                             this.tiles[previous_x, previous_y]._Texture = blue;
                             spriteBatch.Draw(this.tiles[previous_x,previous_y]._Texture, new Vector2(x * 40 + y % 2 * 20 + this.offsetX, y * 30 + this.offsetY), Color.White);
                             this.tiles[x, y]._Texture = red;
                             previous_x = x;
                             previous_y = y;
                             spriteBatch.Draw(this.tiles[x, y]._Texture, new Vector2(x * 40 + y % 2 * 20 + this.offsetX, y * 30 + this.offsetY), Color.White);
                         }
                     }
                }
                
            }
            this.spriteBatch.End();
            base.Draw(gameTime);
        }

        /// <summary>
        /// This two methods just return boolean value for mouse-over or mouse - click
        /// </summary>
        /// <param name="x"></param>
        /// <param name="y"></param>
        /// <returns></returns>
        public bool IsMouseClick(int x, int y)
        {
            MouseState MS = Mouse.GetState();
            if (MS.LeftButton == ButtonState.Pressed)
            {
                return (MS.X >= (x * 40 + y % 2 *20) && MS.X <= (((x + 1)*40)+y %2 *20)  && MS.Y >= y * 30 && MS.Y <= (y + 1) * 30);
            }
            else
            {
                return false;
            }
            
        }

        public bool IsMousePass(int x, int y)
        {
            MouseState MS = Mouse.GetState();
            return (MS.X >= (x * 40 + y % 2 * 20) && MS.X <= (((x + 1) * 40) + y % 2 * 20) && MS.Y >= y * 30 && MS.Y <= (y + 1) * 30);
        }
    }
}
